package com.tanhua.manage.listener;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.tanhua.manage.domain.Log;
import com.tanhua.manage.service.LogService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(topic = "tanhuan-log-151",consumerGroup = "tanhuan-log-151-group")
public class LogMessageListener implements RocketMQListener<String> {

    @Autowired
    private LogService logService;

    // message: 消息内容，如:{"date":"2021-05-17","type":"0101","userId":"1"}
    @Override
    public void onMessage(String message) {
        // 转换
        Map<String,String> map = JSON.parseObject(message, Map.class);
        // 获取消息内容
        String date = map.get("date");
        String type = map.get("type");
        Long userId = Long.parseLong(map.get("userId")+"");
        // 保存日志 - 对象参数
        Log log = new Log();
        log.setUserId(userId);
        log.setLogTime(date);
        log.setType(type);
        log.setCreated(new Date());
        log.setUpdated(new Date());
        // 保存日志
        logService.save(log);
    }
}
